import jwt from "jsonwebtoken";
/**
 * 1、获取token
 * 2、验证token，如果有token，则处理token，再验证token
 */

export default defineEventHandler((event) => {
  let token = getCookie(event, "accessToken");
  if (token) {
    token = token.replace("Bearer ", "");
    let secret = "jbook";
    try {
      let decoded = jwt.verify(token, secret);
      if (typeof decoded !== "string" && decoded.data) {
        event.context.auth = { uid: decoded.data.uid };
      }
    } catch (error) {
      event.context.auth = null;
    }
  }
});
